/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modes;

import navigation.Navigator;
import lejos.util.DebugMessages;
import main.ColorSensorChew;
import main.Engine;
import main.SensorEngine;
import main.SonicSensor;

/**
 * Contains all modes (use cases), which are used by Chewie
 *
 * @author Max
 */
public class ModeController
{

    private ModeBlack black;
    private ModeBlue blue;
    private ModeGreen green;
    private ModeRed red;
    private ModeYellow yellow;
    private ModeDeadEnd deadEnd;
    private ModeFoundObject foundObject;
    /**
     * Object for debug output
     */
    public static DebugMessages dm = new DebugMessages();

    /**
     * Creates the ModeController
     *
     * @param engine
     * @param rotation
     * @param color
     * @param sonic
     * @param navi
     */
    public ModeController(Engine engine, SensorEngine rotation, ColorSensorChew color, SonicSensor sonic, Navigator navi)
    {
        initModes(engine, rotation, color, sonic, navi);
        dm.setLCDLines(7);

    }

    private void initModes(Engine engine, SensorEngine rotation, ColorSensorChew color, SonicSensor sonic, Navigator navi)
    {
        black = new ModeBlack(engine, rotation, color, sonic, navi);
        blue = new ModeBlue(engine, rotation, color, sonic, navi);
        deadEnd = new ModeDeadEnd(engine, rotation, color, sonic, navi);
        foundObject = new ModeFoundObject(engine, rotation, color, sonic, navi);
        green = new ModeGreen(engine, rotation, color, sonic, navi);
        red = new ModeRed(engine, rotation, color, sonic, navi);
        yellow = new ModeYellow(engine, rotation, color, sonic, navi);
    }

    /**
     * Shows the new mode on the NXT-Display and executes the mode
     *
     * @param mode
     * @return new mode
     * @throws InterruptedException
     */
    public Mode changeMode(Mode mode) throws InterruptedException
    {
        //dm.echo(mode+"");
        switch (mode)
        {
            case BLACK:
                mode = black.run();
                break;
            case BLUE:
                mode = blue.run();
                if (mode == Mode.FOUND_OBJECT)
                {
                    foundObject.setMoving(true);
                }
                break;
            case GREEN:
                mode = green.run();
                if (mode == Mode.FOUND_OBJECT)
                {
                    foundObject.setMoving(true);
                } else
                {
                    foundObject.setMoving(false);
                }
                break;
            case RED:
                mode = red.run();
                changeNavi(red.getNavi());
                break;
            case YELLOW:
                mode = yellow.run(false);
                if (mode == Mode.FOUND_OBJECT)
                {
                    foundObject.setMoving(true);
                } else
                {
                    foundObject.setMoving(false);
                }
                break;
            case YELLOW_WITHOUT_CHECK:
                mode = yellow.run(true);
                if (mode == Mode.FOUND_OBJECT)
                {
                    foundObject.setMoving(true);
                } else
                {
                    foundObject.setMoving(false);
                }
                break;
            case DEAD_END:
                mode = deadEnd.run();
                break;
            case FOUND_OBJECT:
                mode = foundObject.run();
                break;
        }
        return mode;
    }

    private void changeNavi(Navigator navi)
    {
        AMode.setNavi(navi);
    }
}
